﻿using EquipSecs300;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Diagnostics.Eventing.Reader;


namespace VisionCore.SECS
{
    public class SecsGlobalVar
    {
        public static Dictionary<string, List<double>> MeasureResList;

        public static bool IsGetPPID { get; set; }

        //recipe名
        public static string SePPID { get; set; }
        //Lot名
        public static string SeLotNO { get; set; }
        public static bool IsClearAlarm { get; set; }
        public static bool IsPause { get; set; }
        public static bool IsResume { get; set; }
        public static bool IsStop { get; set; }

        public static Dictionary<string, bool> LoadPortReadUnLoad { get; set; }

        public static Action<string, List<bool>> AcUpadtaLotSelHandler;

        public static void InitalSetting(SecsEquipHelper secsEquip)
        {
            AttachEvent(secsEquip);
            MeasureResList = new Dictionary<string, List<double>>();
            LoadPortReadUnLoad = new Dictionary<string, bool>
            {
                { "Port_1", false },
                { "Port_2", false }
            };
        }

        public static void AttachEvent(SecsEquipHelper secsEquip)
        {
            secsEquip.RemoteContro.SeClearAlarm += OnSeClearAlarm;
            secsEquip.RemoteContro.SeClearCounter += OnSeClearCounter;
            secsEquip.RemoteContro.SeAcceptCassette += OnSeAcceptCassette;
            secsEquip.RemoteContro.SeRejectCassette += OnSeRejectCassette;
            secsEquip.RemoteContro.SeAcceptWafer += OnSeAcceptWafer;
            secsEquip.RemoteContro.SeRejectWafer += OnSeRejectWafer;
            secsEquip.RemoteContro.SeSelectPPID += OnSeSelectPPID;
            secsEquip.RemoteContro.SeStart += OnSePAStart;
            secsEquip.RemoteContro.SePause += OnSePause;
            secsEquip.RemoteContro.SeResume += OnSeResume;
            secsEquip.RemoteContro.SeStop += OnSeStop;

            secsEquip.PPFormat.FuGetRecipeItemHandler += SecsEQFunc.OnGetRecipeItem;
            secsEquip.PPFormat.FuSetRecipeItemHandler += SecsEQFunc.OnSetRecipeItem;
        }



        public static REMOTE_REPLY_KE OnSeClearAlarm()
        {
            IsClearAlarm = true;
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }

        public static REMOTE_REPLY_KE OnSeClearCounter()
        {

            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }

        public static REMOTE_REPLY_KE OnSeAcceptCassette(string FoupID)
        {
            if (FoupID == "1")
            {
                EquipSecs300.GlobalVar.AcceptCassette["Port_1"] = true;
                return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
            }
            else if (FoupID == "2")
            {
                EquipSecs300.GlobalVar.AcceptCassette["Port_2"] = true;
                return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
            }
            return REMOTE_REPLY_KE.eCOMMAND_PARAMETER_IS_INVALID;
        }

        public static REMOTE_REPLY_KE OnSeRejectCassette(string FoupID)
        {
            if (FoupID == "1")
            {
                EquipSecs300.GlobalVar.RejectCassette["Port_1"] = true;
                return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
            }
            else if (FoupID == "2")
            {
                EquipSecs300.GlobalVar.RejectCassette["Port_2"] = true;
                return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
            }
            return REMOTE_REPLY_KE.eCOMMAND_PARAMETER_IS_INVALID;
        }

        public static REMOTE_REPLY_KE OnSeAcceptWafer(string isAccept, string waferID, string markID, string waferMapName)
        {
            string lotID = "";
            if (waferID.Contains("-"))
            {
                lotID = waferID.Substring(0, waferID.IndexOf("-"));
            }

            string wfMapName = waferMapName;/*.Replace("#", "-");*/
            if (isAccept == "1")
            {
                if (SecsDataHelper.Function.IsUnClientSide)
                {
                    EquipSecs300.GlobalVar.SeAcceptWafer = true;
                    return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
                }
                else
                {
                    if (ParameterHelper.PortA.LotNo.Equals(lotID))
                    {
                        if (SecsEQFunc.CopyMapFile(wfMapName, /*markID*/ ParameterHelper.Aligner.Wafer_ID, ParameterHelper.PortA))
                        {
                            EquipSecs300.GlobalVar.SeAcceptWafer = true;
                            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
                        }
                    }
                    else if (ParameterHelper.PortB.LotNo.Equals(lotID))
                    {
                        if (SecsEQFunc.CopyMapFile(wfMapName, /*markID*/ ParameterHelper.Aligner.Wafer_ID, ParameterHelper.PortB))
                        {
                            EquipSecs300.GlobalVar.SeAcceptWafer = true;
                            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
                        }
                    }
                    else
                    {
                        //SeLogMsg($"WaferID上报EAP回复结果 -> waferID LotNO：{waferID}与本地waferID LotNO：{ParameterHelper.Aligner.Wafer_ID} 不符");
                        //SeLogMsg($"PA LotNO：{ParameterHelper.PortA.LotNo}，PB LotNO：{ParameterHelper.PortB.LotNo}");
                    }
                }

            }
            return REMOTE_REPLY_KE.eCOMMAND_OTHER;
        }

        public static REMOTE_REPLY_KE OnSeRejectWafer()
        {
            EquipSecs300.GlobalVar.SeRejectWafer = true;
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }

        public static REMOTE_REPLY_KE OnSeSelectPPID(string portNO, string PPID, string lotNO)
        {
            if (string.IsNullOrEmpty(portNO) || string.IsNullOrEmpty(lotNO))
            {
                return REMOTE_REPLY_KE.eCOMMAND_PARAMETER_IS_INVALID;
            }
            if (portNO == "1")
            {
                IsGetPPID = true;
                SePPID = PPID;
                SeLotNO = lotNO;
                return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
            }
            else if (portNO == "2")
            {
                IsGetPPID = true;
                SePPID = PPID;
                SeLotNO = lotNO;
                return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
            }
            else
            {
                return REMOTE_REPLY_KE.eCOMMAND_PARAMETER_IS_INVALID;
            }

        }

        public static REMOTE_REPLY_KE OnSePAStart(string FoupID)
        {
            if (FoupID == "1")
            {
                EquipSecs300.GlobalVar.StartScan["Port_1"] = true;
                return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
            }
            else if (FoupID == "2")
            {
                EquipSecs300.GlobalVar.StartScan["Port_2"] = true;
                return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
            }
            return REMOTE_REPLY_KE.eCOMMAND_PARAMETER_IS_INVALID;

        }

        public static REMOTE_REPLY_KE OnSePause()
        {
            IsPause = true;
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }
        public static REMOTE_REPLY_KE OnSeResume()
        {
            IsResume = true;
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }
        public static REMOTE_REPLY_KE OnSeStop()
        {
            IsStop = true;
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }
        //private static void SeLogMsg(string strLogMsg)
        //{
        //    LogHelper.Write(LogHelper.Module.Procedure, "SECS : " + strLogMsg);
        //}
    }
}
